package com.controller.purchase;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.ListUtils;
import com.common.DateUtils;
import com.common.annotation.RequestCheck;
import com.common.excel.DownloadData;
import com.common.http.HttpResult;
import com.model.purchase.PurchaseInquiry;
import com.model.purchase.PurchaseInquiryVO;
import com.service.purchase.InquiryService;
import com.service.purchase.ProgramService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 询价书管理
 */
@Controller
@RequestMapping(value = "/api")
public class InquiryController {

	static final Logger logger = LoggerFactory.getLogger(InquiryController.class);

	@Autowired
	InquiryService inquiryService;

	

	/**
	 * 查询列表
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/purchase/selectInquiryList", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult selectInquiryList(@RequestParam Map<String, Object> map) {
		return inquiryService.selectInquiryList(map);
	}

	/**
	 * 查询详情
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/purchase/selectInquiry", method = { RequestMethod.GET })
	@ResponseBody
	public HttpResult selectInquiry(@RequestParam String inqId) {
		return inquiryService.selectInquiry(inqId);
	}

	/**
	 * 保存询价
	 * 
	 * @param purchaseInquiryVO
	 * @return
	 */
	@RequestMapping(value = "/purchase/saveInquiry", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult saveInquiry(@RequestBody PurchaseInquiryVO purchaseInquiryVO) {
		return inquiryService.saveInquiry(purchaseInquiryVO);
	}

	/**
	 * 提交询价
	 * 
	 * @param purchaseInquiryVO
	 * @return
	 */
//	@RequestMapping(value = "/purchase/commitInquiry", method = { RequestMethod.POST })
//	@ResponseBody
//	public HttpResult commitInquiry(@RequestBody PurchaseInquiryVO purchaseInquiryVO) {
//		return inquiryService.commitInquiry(purchaseInquiryVO);
//	}

	/**
	 * 回退询价
	 * 
	 * @param purchaseInquiryVO
	 * @return
	 */
	@RequestMapping(value = "/purchase/backInquiry", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult backInquiry(@RequestBody PurchaseInquiryVO purchaseInquiryVO) {
		return inquiryService.backInquiry(purchaseInquiryVO);
	}

	/**
	 * 删除询价
	 * 
	 * @param inqId
	 * @return
	 */
	@RequestMapping(value = "/purchase/delInquiry", method = { RequestMethod.DELETE })
	@ResponseBody
	public HttpResult delInquiry(@RequestParam String inqId) {
		return inquiryService.delInquiry(inqId);
	}

	/**
	 * 导出
	 * 
	 * @param map
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "/purchase/export")
	public void export(@RequestParam Map<String, Object> map, HttpServletResponse response) throws IOException {
//		ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//		HttpServletResponse response = attributes.getResponse();	
		response.setContentType("application/vnd.ms-excel");
		response.setCharacterEncoding("utf-8");
		// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
		String fileName = URLEncoder.encode("询价书-" + DateUtils.getCurrentDate(), "UTF-8");
		response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
		inquiryService.exportInquiryList(map, response);
//		EasyExcel.write(response.getOutputStream(), PurchaseInquiry.class).sheet("模板").doWrite(data());

	}

	private List<DownloadData> data() {
		List<DownloadData> list = ListUtils.newArrayList();
		for (int i = 0; i < 10; i++) {
			DownloadData data = new DownloadData();
			data.setString("字符串" + 0);
			data.setDate(new Date());
			data.setDoubleData(0.56);
			list.add(data);
		}
		return list;
	}
}
